<?php
namespace BLL\DAO;
use BLL\DataModel;

use BLL\DataModel\DataType as DataType;
/*
 *  
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2015 Max Group. All rights reserved.
 * 
 */

/**
 * Description of DataModelBuiltTableAdapter
 *
 * @author david
 */
class DataModelBuiltTableAdapter{
    private $model = null;
    
    public function __construct($dataModel) {
        $this->model = $dataModel;
    }
    
    public function builtTable($table) {
        $table->string($this->model->idField,15);
        $table->primary($this->model->idField);
        if ($this->model->masterNode) {
            $table->string($this->model->masterIDField,15);
        }
        if ($this->model->getRootIDField() && 
                $this->model->getRootIDField() != $this->model->masterIDField && 
                $this->model->getRootIDField() != $this->model->idField) {
            $table->string($this->model->getRootIDField(),15);
        }
        $table->primaryKey = $this->model->idField;
        $unique = array();
        foreach ($this->model->items as $item){
            if (strcasecmp($item->fieldName, $this->model->idField) === 0 
                    || strcasecmp($item->fieldName, $this->model->masterIDField) === 0
                    || strcasecmp($item->fieldName, $this->model->getRootIDField()) === 0) {
                continue;
            }
            $col = null;
            if($item->dataType === DataType::Int){
                 $col = $table->integer($item->fieldName);
            }
            if($item->dataType === DataType::BigInt){
                 $col = $table->bigInteger($item->fieldName);
            }
            if($item->dataType === DataType::Text){
                 $col = $table->text($item->fieldName);
            }
            if($item->dataType === DataType::Bool){
                 $col = $table->boolean($item->fieldName);
            }
            if($item->dataType === DataType::Currency){
                 $col = $table->decimal($item->fieldName,10,2);
            }
            if($item->dataType === DataType::Date){
                 $col = $table->date($item->fieldName);
            }
            if($item->dataType === DataType::DateTime){
                 $col = $table->datetime($item->fieldName);
            }
            if($item->dataType === DataType::Time){
                 $col = $table->time($item->fieldName);
            }
            if($item->dataType === DataType::String){
                 $col = $table->string($item->fieldName, $item->dataLen);
            }
            if (!$item->isNecessary) {
                $col = $col->nullable();
            }
            if ($item->uniqueGroup) {
                if (!isset($unique[$item->uniqueGroup])) {
                    $unique[$item->uniqueGroup] = array();
                    if ($this->model->masterNode) {
                        array_push($unique[$item->uniqueGroup],$this->model->masterIDField);
                    }
                }
                array_push($unique[$item->uniqueGroup],$item->fieldName);
            }
        }
        foreach ($unique as $un){
            $table->unique($un);
        }
        if ($this->model->masterNode) {
            $master = $this->model->masterNode->getRealTableName();
            $table->foreign($this->model->masterIDField)
                ->references($this->model->masterNode->idField)->on($master)
                ->onDelete('cascade');
        }
    }

}
